import { defineStore } from "pinia";
import { reqPlyData, reqUserAddress, reqSumbetPay } from "@/API/APIs/api";
import { ElNotification } from "element-plus";
import TypeSum from '@/TypeSum/entry'
interface BeforPayDataType {
    PlyData: any, address: any[], orderID?: number
}

export const useBeforPay = defineStore('BeforPay', {
    state: (): BeforPayDataType => ({
        PlyData: {}, address: [], orderID: 0
    }),
    getters: {
        detailArrayList(state) {
            return Object.keys(state.PlyData).length > 0 ? state.PlyData.detailArrayList : []
        },
        orderDetailVoList(state) {
            return Object.keys(state.PlyData).length > 0 ? state.PlyData.orderDetailVoList : []
        },
        idDefultAddress(state) {
            return state.address.filter((value) => value.isDefault == '1')[0] || {}
        }
    },
    actions: {
        async getPlyData() {
            const result = await reqPlyData() as TypeSum['AxiosData']['AxiosData']
            if (result.code == 200) {
                this.PlyData = result.data
            } else {
                ElNotification.error({
                    title: '错误',
                    message: result.message
                })
            }
        },
        async getUserAddress() {
            const result = await reqUserAddress() as TypeSum['AxiosData']['AxiosData']
            if (result.code == 200) {
                this.address = result.data
            } else {
                ElNotification.error({
                    title: '错误',
                    message: result.message
                })
            }
        },
        async getPushPlay(tradeNo: string, data: { consignee: string, consigneeTel: number, deliveryAddress: string, paymentWay: 'ONLINE', orderComment: string, orderDetailList: any[] }) {
            const result = await reqSumbetPay(tradeNo, data) as TypeSum['AxiosData']['AxiosData']
            if (result.code == 200) {
                this.orderID = result.data
                return result.data
            } else {
                ElNotification.error({
                    title: '错误',
                    message: result.message
                })
                return false
            }
        }
    }
})